package com.app.bhca.demo.tjcahttpdemo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class TJCAHttp {
    private static TJCAHttp uniqueInstance;
    private static String tjcaurl = "http://localhost:38877/tjca/gmt0020/";

    private static Map<String,Object> params = null;

    private static String cert; //证书
    private static String login_type; //
    private static String stamp_img;  //签名图片
    private static String timestamp;  //时间戳


    private TJCAHttp(){

    }
    public static TJCAHttp GetInstance(){
        if (uniqueInstance == null){
            uniqueInstance = new TJCAHttp();
        }
        return  uniqueInstance;
    }


    /**
     * 多种方式登录
     * @param type
     * @return
     */
    public  String BHCA_Login(int type){
        params = new HashMap<String , Object>();
        params.put("func_name","BHCA_Login");
        params.put("param0",type);

        return PostDataResult(tjcaurl,params);
    }



    /**
     * 多种方式签名（UKEY/软盾/手机盾）
     * @param type
     * @param InData
     * @return
     */
    public String BHCA_SignData(int type, String InData){
        params = new HashMap<String, Object>();
        params.put("func_name","BHCA_SignData");
        params.put("param0",type);
        params.put("param1",base64.Base64Encode(InData));

        return PostDataResult(tjcaurl,params);
    }

    /**
     * 获取使用者唯一标识
     * @param type
     * @return
     */
    public String BHCA_UniqueID(int type){
        params = new HashMap<String , Object>();
        params.put("func_name","BHCA_UniqueID");
        params.put("param0",type);

        return PostDataResult(tjcaurl,params);
    }

    /**
     * 获得证书信息
     * @param Base64EncodeCert
     * @param type
     * @return
     */
    public String SOF_GetCertInfo(String Base64EncodeCert, Short type){
        params = new HashMap<String, Object>();
        params.put("func_name","SOF_GetCertInfo");
        params.put("param0",Base64EncodeCert);
        params.put("param1",type);

        return PostDataResult(tjcaurl,params);
    }

    /**
     * 获得证书扩展信息OID
     * @param Base64EncodeCert
     * @param Oid
     * @return
     */
    public String SOF_GetCertInfoByOid(String Base64EncodeCert, String Oid){
        params = new HashMap<String, Object>();
        params.put("func_name","SOF_GetCertInfoByOid");
        params.put("param0",Base64EncodeCert);
        params.put("param1",Oid);

        return PostDataResult(tjcaurl,params);
    }

    /**
     * 验证签名
     * @param Base64EncodeCert
     * @param InData
     * @param SignValue
     * @return
     */
    public boolean SOF_VerifySignedData(String Base64EncodeCert, String InData, String SignValue){
        params = new HashMap<String, Object>();
        params.put("func_name","SOF_VerifySignedData");
        params.put("param0",Base64EncodeCert);
        params.put("param1",InData);
        params.put("param2",SignValue);

        boolean isTrue = (PostDataResult(tjcaurl,params) == "true") ? true : false;

        return isTrue;
    }


    /**
     * 发送本地POST请求
     * @param LocalUrl
     * @param ParamsMap
     * @return
     */
    public static String PostDataResult(String LocalUrl,Map<String,Object> ParamsMap){

        String json = JSON.toJSONString(ParamsMap, true);

        String res = HttpHelp.doPost(LocalUrl,json);
//        System.out.println(res);
        JSONObject jsonObject = JSONObject.parseObject(res);
        String content = jsonObject.getString("content");
//        System.out.println(content);
        return base64.Base64Decode(content);
    }


}
